package com.framework.loippi.service.impl;

import com.framework.loippi.dao.TUserSettingDao;
import com.framework.loippi.entity.TUserSetting;
import com.framework.loippi.service.TUserSettingService;
import com.framework.loippi.support.Setting;
import com.framework.loippi.utils.CacheUtils;
import com.framework.loippi.utils.SettingUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SERVICE - TUserSetting(用户设置数据)
 *
 * @author longbh
 * @version 2.0
 */
@Service
public class TUserSettingServiceImpl extends GenericServiceImpl<TUserSetting, Long> implements TUserSettingService {

    @Autowired
    private TUserSettingDao tUserSettingDao;

    @Autowired
    public void setGenericDao() {
        super.setGenericDao(tUserSettingDao);
    }

    @Override
    public String find(String key) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("cKey", key);
        List<TUserSetting> result = tUserSettingDao.findByParams(map);
        return result != null && !result.isEmpty() ? result.get(0).getCValue() : "";
    }

    @Override
    public Setting get() {
        Setting setting = (Setting) CacheUtils.get().get("setting");
        if (setting == null) {
            setting = new Setting();
            List<TUserSetting> userSettings = findAll();
            Map<String, TUserSetting> maps = new HashMap<>();
            for (TUserSetting userSetting : userSettings) {
                maps.put(userSetting.getCKey(), userSetting);
            }


            TUserSetting siteName = maps.get("siteName");
            if (siteName == null) {
                setting.setSiteName(SettingUtils.get().getSiteName());
            } else {
                setting.setSiteName(siteName.getCValue());
            }

            TUserSetting siteUrl = maps.get("siteUrl");
            if (siteUrl == null) {
                setting.setSiteUrl(SettingUtils.get().getSiteUrl());
            } else {
                setting.setSiteUrl(siteUrl.getCValue());
            }

            TUserSetting logo = maps.get("logo");
            if (logo == null) {
                setting.setLogo(SettingUtils.get().getLogo());
            } else {
                setting.setLogo(logo.getCValue());
            }

            TUserSetting contact = maps.get("contact");
            if (contact == null) {
                setting.setContact(SettingUtils.get().getContact());
            } else {
                setting.setContact(contact.getCValue());
            }

            TUserSetting address = maps.get("address");
            if (address == null) {
                setting.setAddress(SettingUtils.get().getAddress());
            } else {
                setting.setAddress(address.getCValue());
            }

            TUserSetting phone = maps.get("phone");
            if (phone == null) {
                setting.setPhone(SettingUtils.get().getPhone());
            } else {
                setting.setPhone(phone.getCValue());
            }

            TUserSetting email = maps.get("email");
            if (email == null) {
                setting.setEmail(SettingUtils.get().getEmail());
            } else {
                setting.setEmail(email.getCValue());
            }

            TUserSetting certtext = maps.get("certtext");
            if (certtext == null) {
                setting.setCerttext(SettingUtils.get().getCerttext());
            } else {
                setting.setCerttext(certtext.getCValue());
            }


            TUserSetting accountLockCount = maps.get("accountLockCount");
            if (accountLockCount == null) {
                setting.setAccountLockCount(SettingUtils.get().getAccountLockCount());
            } else {
                setting.setAccountLockCount(Integer.valueOf(accountLockCount.getCValue()));
            }

            TUserSetting accountLockTime = maps.get("accountLockTime");
            if (accountLockTime == null) {
                setting.setAccountLockTime(SettingUtils.get().getAccountLockTime());
            } else {
                setting.setAccountLockTime(Integer.valueOf(accountLockTime.getCValue()));
            }

            TUserSetting uploadMaxSize = maps.get("uploadMaxSize");
            if (uploadMaxSize == null) {
                setting.setUploadMaxSize(SettingUtils.get().getUploadMaxSize());
            } else {
                setting.setUploadMaxSize(Integer.valueOf(uploadMaxSize.getCValue()));
            }

            TUserSetting uploadImageExtension = maps.get("uploadImageExtension");
            if (uploadImageExtension == null) {
                setting.setUploadImageExtension(SettingUtils.get().getUploadImageExtension());
            } else {
                setting.setUploadImageExtension(uploadImageExtension.getCValue());
            }


            TUserSetting uploadFlashExtension = maps.get("uploadFlashExtension");
            if (uploadFlashExtension == null) {
                setting.setUploadFlashExtension(SettingUtils.get().getUploadFlashExtension());
            } else {
                setting.setUploadFlashExtension(uploadFlashExtension.getCValue());
            }

            TUserSetting uploadMediaExtension = maps.get("uploadMediaExtension");
            if (uploadMediaExtension == null) {
                setting.setUploadMediaExtension(SettingUtils.get().getUploadMediaExtension());
            } else {
                setting.setUploadMediaExtension(uploadMediaExtension.getCValue());
            }

            TUserSetting uploadFileExtension = maps.get("uploadFileExtension");
            if (uploadFileExtension == null) {
                setting.setUploadFileExtension(SettingUtils.get().getUploadFileExtension());
            } else {
                setting.setUploadFileExtension(uploadFileExtension.getCValue());
            }

            TUserSetting imageUploadPath = maps.get("imageUploadPath");
            if (imageUploadPath == null) {
                setting.setImageUploadPath(SettingUtils.get().getImageUploadPath());
            } else {
                setting.setImageUploadPath(imageUploadPath.getCValue());
            }

            TUserSetting flashUploadPath = maps.get("flashUploadPath");
            if (flashUploadPath == null) {
                setting.setFlashUploadPath(SettingUtils.get().getFlashUploadPath());
            } else {
                setting.setFlashUploadPath(flashUploadPath.getCValue());
            }

            TUserSetting mediaUploadPath = maps.get("mediaUploadPath");
            if (mediaUploadPath == null) {
                setting.setMediaUploadPath(SettingUtils.get().getMediaUploadPath());
            } else {
                setting.setMediaUploadPath(mediaUploadPath.getCValue());
            }


            TUserSetting fileUploadPath = maps.get("fileUploadPath");
            if (fileUploadPath == null) {
                setting.setFileUploadPath(SettingUtils.get().getFileUploadPath());
            } else {
                setting.setFileUploadPath(fileUploadPath.getCValue());
            }

            TUserSetting priceScale = maps.get("priceScale");
            if (priceScale == null) {
                setting.setPriceScale(SettingUtils.get().getPriceScale());
            } else {
                setting.setPriceScale(Integer.valueOf(priceScale.getCValue()));
            }

            TUserSetting priceRoundType = maps.get("priceRoundType");
            if (priceRoundType == null) {
                setting.setPriceRoundType(SettingUtils.get().getPriceRoundType());
            } else {
                Setting.RoundType roundType = Setting.RoundType.valueOf(priceRoundType.getCValue());
                setting.setPriceRoundType(roundType);
            }

            TUserSetting currencySign = maps.get("currencySign");
            if (currencySign == null) {
                setting.setCurrencySign(SettingUtils.get().getCurrencySign());
            } else {
                setting.setCurrencySign(currencySign.getCValue());
            }

            TUserSetting currencyUnit = maps.get("currencyUnit");
            if (currencyUnit == null) {
                setting.setCurrencyUnit(SettingUtils.get().getCurrencyUnit());
            } else {
                setting.setCurrencyUnit(currencyUnit.getCValue());
            }

            TUserSetting smtpFromMail = maps.get("smtpFromMail");
            if (smtpFromMail == null) {
                setting.setSmtpFromMail(SettingUtils.get().getSmtpFromMail());
            } else {
                setting.setSmtpFromMail(smtpFromMail.getCValue());
            }

            TUserSetting smtpHost = maps.get("smtpHost");
            if (smtpHost == null) {
                setting.setSmtpHost(SettingUtils.get().getSmtpHost());
            } else {
                setting.setSmtpHost(smtpHost.getCValue());
            }

            TUserSetting smtpPort = maps.get("smtpPort");
            if (smtpPort == null) {
                setting.setSmtpPort(SettingUtils.get().getSmtpPort());
            } else {
                setting.setSmtpPort(Integer.valueOf(smtpPort.getCValue()));
            }

            TUserSetting smtpUsername = maps.get("smtpUsername");
            if (smtpUsername == null) {
                setting.setSmtpUsername(SettingUtils.get().getSmtpUsername());
            } else {
                setting.setSmtpUsername(smtpUsername.getCValue());
            }

            TUserSetting smtpPassword = maps.get("smtpPassword");
            if (smtpPassword == null) {
                setting.setSmtpPassword(SettingUtils.get().getSmtpPassword());
            } else {
                setting.setSmtpPassword(smtpPassword.getCValue());
            }

            TUserSetting cookiePath = maps.get("cookiePath");
            if (cookiePath == null) {
                setting.setCookiePath(SettingUtils.get().getCookiePath());
            } else {
                setting.setCookiePath(cookiePath.getCValue());
            }

            TUserSetting cookieDomain = maps.get("cookieDomain");
            if (cookieDomain == null) {
                setting.setCookieDomain(SettingUtils.get().getCookieDomain());
            } else {
                setting.setCookieDomain(cookieDomain.getCValue());
            }

            TUserSetting isDevelopmentEnabled = maps.get("isDevelopmentEnabled");
            if (isDevelopmentEnabled == null) {
                setting.setIsDevelopmentEnabled(SettingUtils.get().getIsDevelopmentEnabled());
            } else {
                setting.setIsDevelopmentEnabled(Boolean.valueOf(isDevelopmentEnabled.getCValue()));
            }

            TUserSetting registorPoint = maps.get("registorPoint");
            if (registorPoint == null) {
                setting.setRegistorPoint(SettingUtils.get().getRegistorPoint());
            } else {
                setting.setRegistorPoint(Integer.valueOf(registorPoint.getCValue()));
            }

            TUserSetting oldProfile = maps.get("oldProfile");
            if (oldProfile == null) {
                setting.setOldProfile(SettingUtils.get().getOldProfile());
            } else {
                setting.setOldProfile(Integer.valueOf(oldProfile.getCValue()));
            }

            TUserSetting addCustom = maps.get("addCustom");
            if (addCustom == null) {
                setting.setAddCustom(SettingUtils.get().getAddCustom());
            } else {
                setting.setAddCustom(Integer.valueOf(addCustom.getCValue()));
            }

            TUserSetting addCheck = maps.get("addCheck");
            if (addCheck == null) {
                setting.setAddCheck(SettingUtils.get().getAddCheck());
            } else {
                setting.setAddCheck(Integer.valueOf(addCheck.getCValue()));
            }

            TUserSetting addShare = maps.get("addShare");
            if (addShare == null) {
                setting.setAddShare(SettingUtils.get().getAddShare());
            } else {
                setting.setAddShare(Integer.valueOf(addShare.getCValue()));
            }

            TUserSetting buyPoint = maps.get("buyPoint");
            if (buyPoint == null) {
                setting.setBuyPoint(SettingUtils.get().getBuyPoint());
            } else {
                setting.setBuyPoint(Integer.valueOf(buyPoint.getCValue()));
            }

            TUserSetting sysTime = maps.get("sysTime");
            if (sysTime == null) {
                setting.setSysTime(SettingUtils.get().getSysTime());
            } else {
                setting.setSysTime(Long.valueOf(sysTime.getCValue()));
            }

            TUserSetting enableMachine = maps.get("enableMachine");
            if (enableMachine == null) {
                setting.setEnableMachine(SettingUtils.get().getEnableMachine());
            } else {
                setting.setEnableMachine(Boolean.valueOf(enableMachine.getCValue()));
            }

            TUserSetting totalUser = maps.get("totalUser");
            if (totalUser == null) {
                setting.setTotalUser(SettingUtils.get().getTotalUser());
            } else {
                setting.setTotalUser(Integer.valueOf(totalUser.getCValue()));
            }

            TUserSetting userBaseCount = maps.get("userBaseCount");
            if (userBaseCount == null) {
                setting.setUserBaseCount(SettingUtils.get().getUserBaseCount());
            } else {
                setting.setUserBaseCount(Integer.valueOf(userBaseCount.getCValue()));
            }

            TUserSetting randomStart = maps.get("randomStart");
            if (randomStart == null) {
                setting.setRandomStart(SettingUtils.get().getRandomStart());
            } else {
                setting.setRandomStart(Integer.valueOf(randomStart.getCValue()));
            }

            TUserSetting randomEnd = maps.get("randomEnd");
            if (randomEnd == null) {
                setting.setRandomEnd(SettingUtils.get().getRandomEnd());
            } else {
                setting.setRandomEnd(Integer.valueOf(randomEnd.getCValue()));
            }

            TUserSetting qrCodeImagePath = maps.get("qrCodeImagePath");
            if (qrCodeImagePath == null) {
                setting.setQrCodeImagePath(SettingUtils.get().getQrCodeImagePath());
            } else {
                setting.setQrCodeImagePath(qrCodeImagePath.getCValue());
            }

            TUserSetting enableSystem = maps.get("enableSystem");
            if (enableSystem == null) {
                setting.setEnableSystem(SettingUtils.get().getEnableSystem());
            } else {
                setting.setEnableSystem(Boolean.valueOf(enableSystem.getCValue()));
            }

            TUserSetting enableBuySystem = maps.get("enableBuySystem");
            if (enableBuySystem == null) {
                setting.setEnableBuySystem(SettingUtils.get().getEnableBuySystem());
            } else {
                setting.setEnableBuySystem(Boolean.valueOf(enableBuySystem.getCValue()));
            }

            TUserSetting captchaTypes = maps.get("captchaTypes");
            if (captchaTypes == null) {
                setting.setCaptchaTypes(SettingUtils.get().getCaptchaTypes());
            } else {
                if (captchaTypes.getCValue() != null) {
                    String[] typeArray = captchaTypes.getCValue().split(",");
                    Setting.CaptchaType[] type = new Setting.CaptchaType[typeArray.length];
                    int i = 0;
                    for (String item : typeArray) {
                        type[i] = Setting.CaptchaType.valueOf(item);
                        i++;
                    }
                    setting.setCaptchaTypes(type);
                }
            }


            CacheUtils.get().add("setting", setting, 60);
        }
        return setting;
    }

    @Override
    public void set(Setting setting) {
        List<TUserSetting> userSettings = findAll();
        Map<String, TUserSetting> maps = new HashMap<>();
        for (TUserSetting userSetting : userSettings) {
            maps.put(userSetting.getCKey(), userSetting);
        }

        TUserSetting siteName = maps.get("siteName");
        if (siteName == null) {
            siteName = new TUserSetting();
            siteName.setCKey("siteName");
            siteName.setCValue(setting.getSiteName() + "");
            save(siteName);
        } else {
            siteName.setCValue(setting.getSiteName() + "");
            update(siteName);
        }

        TUserSetting siteUrl = maps.get("siteUrl");
        if (siteUrl == null) {
            siteUrl = new TUserSetting();
            siteUrl.setCKey("siteUrl");
            siteUrl.setCValue(setting.getSiteUrl() + "");
            save(siteUrl);
        } else {
            siteUrl.setCValue(setting.getSiteUrl() + "");
            update(siteUrl);
        }

        TUserSetting logo = maps.get("logo");
        if (logo == null) {
            logo = new TUserSetting();
            logo.setCKey("logo");
            logo.setCValue(setting.getLogo() + "");
            save(logo);
        } else {
            logo.setCValue(setting.getLogo() + "");
            update(logo);
        }

        TUserSetting contact = maps.get("contact");
        if (contact == null) {
            contact = new TUserSetting();
            contact.setCKey("contact");
            contact.setCValue(setting.getContact() + "");
            save(contact);
        } else {
            contact.setCValue(setting.getContact() + "");
            update(contact);
        }

        TUserSetting address = maps.get("address");
        if (address == null) {
            address = new TUserSetting();
            address.setCKey("address");
            address.setCValue(setting.getAddress() + "");
            save(address);
        } else {
            address.setCValue(setting.getAddress() + "");
            update(address);
        }

        TUserSetting phone = maps.get("phone");
        if (phone == null) {
            phone = new TUserSetting();
            phone.setCKey("phone");
            phone.setCValue(setting.getPhone() + "");
            save(phone);
        } else {
            phone.setCValue(setting.getPhone() + "");
            update(phone);
        }

        TUserSetting email = maps.get("email");
        if (email == null) {
            email = new TUserSetting();
            email.setCKey("email");
            email.setCValue(setting.getEmail() + "");
            save(email);
        } else {
            email.setCValue(setting.getEmail() + "");
            update(email);
        }

        TUserSetting certtext = maps.get("certtext");
        if (certtext == null) {
            certtext = new TUserSetting();
            certtext.setCKey("certtext");
            certtext.setCValue(setting.getCerttext() + "");
            save(certtext);
        } else {
            certtext.setCValue(setting.getCerttext() + "");
            update(certtext);
        }


        TUserSetting accountLockCount = maps.get("accountLockCount");
        if (accountLockCount == null) {
            accountLockCount = new TUserSetting();
            accountLockCount.setCKey("accountLockCount");
            accountLockCount.setCValue(setting.getAccountLockCount() + "");
            save(accountLockCount);
        } else {
            accountLockCount.setCValue(setting.getAccountLockCount() + "");
            update(accountLockCount);
        }

        TUserSetting accountLockTime = maps.get("accountLockTime");
        if (accountLockTime == null) {
            accountLockTime = new TUserSetting();
            accountLockTime.setCKey("accountLockTime");
            accountLockTime.setCValue(setting.getAccountLockTime() + "");
            save(accountLockTime);
        } else {
            accountLockTime.setCValue(setting.getAccountLockTime() + "");
            update(accountLockTime);
        }

        TUserSetting uploadMaxSize = maps.get("uploadMaxSize");
        if (uploadMaxSize == null) {
            uploadMaxSize = new TUserSetting();
            uploadMaxSize.setCKey("uploadMaxSize");
            uploadMaxSize.setCValue(setting.getUploadMaxSize() + "");
            save(uploadMaxSize);
        } else {
            uploadMaxSize.setCValue(setting.getUploadMaxSize() + "");
            update(uploadMaxSize);
        }

        TUserSetting uploadImageExtension = maps.get("uploadImageExtension");
        if (uploadImageExtension == null) {
            uploadImageExtension = new TUserSetting();
            uploadImageExtension.setCKey("uploadImageExtension");
            uploadImageExtension.setCValue(setting.getUploadImageExtension() + "");
            save(uploadImageExtension);
        } else {
            uploadImageExtension.setCValue(setting.getUploadImageExtension() + "");
            update(uploadImageExtension);
        }


        TUserSetting uploadFlashExtension = maps.get("uploadFlashExtension");
        if (uploadFlashExtension == null) {
            uploadFlashExtension = new TUserSetting();
            uploadFlashExtension.setCKey("uploadFlashExtension");
            uploadFlashExtension.setCValue(setting.getUploadFlashExtension() + "");
            save(uploadFlashExtension);
        } else {
            uploadFlashExtension.setCValue(setting.getUploadFlashExtension() + "");
            update(uploadFlashExtension);
        }

        TUserSetting uploadMediaExtension = maps.get("uploadMediaExtension");
        if (uploadMediaExtension == null) {
            uploadMediaExtension = new TUserSetting();
            uploadMediaExtension.setCKey("uploadMediaExtension");
            uploadMediaExtension.setCValue(setting.getUploadMediaExtension() + "");
            save(uploadMediaExtension);
        } else {
            uploadMediaExtension.setCValue(setting.getUploadMediaExtension() + "");
            update(uploadMediaExtension);
        }

        TUserSetting uploadFileExtension = maps.get("uploadFileExtension");
        if (uploadFileExtension == null) {
            uploadFileExtension = new TUserSetting();
            uploadFileExtension.setCKey("uploadFileExtension");
            uploadFileExtension.setCValue(setting.getUploadFileExtension() + "");
            save(uploadFileExtension);
        } else {
            uploadFileExtension.setCValue(setting.getUploadFileExtension() + "");
            update(uploadFileExtension);
        }

        TUserSetting imageUploadPath = maps.get("imageUploadPath");
        if (imageUploadPath == null) {
            imageUploadPath = new TUserSetting();
            imageUploadPath.setCKey("imageUploadPath");
            imageUploadPath.setCValue(setting.getImageUploadPath() + "");
            save(imageUploadPath);
        } else {
            imageUploadPath.setCValue(setting.getImageUploadPath() + "");
            update(imageUploadPath);
        }

        TUserSetting flashUploadPath = maps.get("flashUploadPath");
        if (flashUploadPath == null) {
            flashUploadPath = new TUserSetting();
            flashUploadPath.setCKey("flashUploadPath");
            flashUploadPath.setCValue(setting.getFlashUploadPath() + "");
            save(flashUploadPath);
        } else {
            flashUploadPath.setCValue(setting.getFlashUploadPath() + "");
            update(flashUploadPath);
        }

        TUserSetting mediaUploadPath = maps.get("mediaUploadPath");
        if (mediaUploadPath == null) {
            mediaUploadPath = new TUserSetting();
            mediaUploadPath.setCKey("mediaUploadPath");
            mediaUploadPath.setCValue(setting.getMediaUploadPath() + "");
            save(mediaUploadPath);
        } else {
            mediaUploadPath.setCValue(setting.getMediaUploadPath() + "");
            update(mediaUploadPath);
        }


        TUserSetting fileUploadPath = maps.get("fileUploadPath");
        if (fileUploadPath == null) {
            fileUploadPath = new TUserSetting();
            fileUploadPath.setCKey("fileUploadPath");
            fileUploadPath.setCValue(setting.getFileUploadPath() + "");
            save(fileUploadPath);
        } else {
            fileUploadPath.setCValue(setting.getFileUploadPath() + "");
            update(fileUploadPath);
        }

        TUserSetting priceScale = maps.get("priceScale");
        if (priceScale == null) {
            priceScale = new TUserSetting();
            priceScale.setCKey("priceScale");
            priceScale.setCValue(setting.getPriceScale() + "");
            save(priceScale);
        } else {
            priceScale.setCValue(setting.getPriceScale() + "");
            update(priceScale);
        }

        TUserSetting priceRoundType = maps.get("priceRoundType");
        if (priceRoundType == null) {
            priceRoundType = new TUserSetting();
            priceRoundType.setCKey("priceRoundType");
            priceRoundType.setCValue(setting.getPriceRoundType() + "");
            save(priceRoundType);
        } else {
            priceRoundType.setCValue(setting.getPriceRoundType() + "");
            update(priceRoundType);
        }

        TUserSetting currencySign = maps.get("currencySign");
        if (currencySign == null) {
            currencySign = new TUserSetting();
            currencySign.setCKey("currencySign");
            currencySign.setCValue(setting.getCurrencySign() + "");
            save(currencySign);
        } else {
            currencySign.setCValue(setting.getCurrencySign() + "");
            update(currencySign);
        }

        TUserSetting currencyUnit = maps.get("currencyUnit");
        if (currencyUnit == null) {
            currencyUnit = new TUserSetting();
            currencyUnit.setCKey("currencyUnit");
            currencyUnit.setCValue(setting.getCurrencyUnit() + "");
            save(currencyUnit);
        } else {
            currencyUnit.setCValue(setting.getCurrencyUnit() + "");
            update(currencyUnit);
        }

        TUserSetting smtpFromMail = maps.get("smtpFromMail");
        if (smtpFromMail == null) {
            smtpFromMail = new TUserSetting();
            smtpFromMail.setCKey("smtpFromMail");
            smtpFromMail.setCValue(setting.getSmtpFromMail() + "");
            save(smtpFromMail);
        } else {
            smtpFromMail.setCValue(setting.getSmtpFromMail() + "");
            update(smtpFromMail);
        }

        TUserSetting smtpHost = maps.get("smtpHost");
        if (smtpHost == null) {
            smtpHost = new TUserSetting();
            smtpHost.setCKey("smtpHost");
            smtpHost.setCValue(setting.getSmtpHost() + "");
            save(smtpHost);
        } else {
            smtpHost.setCValue(setting.getSmtpHost() + "");
            update(smtpHost);
        }

        TUserSetting smtpPort = maps.get("smtpPort");
        if (smtpPort == null) {
            smtpPort = new TUserSetting();
            smtpPort.setCKey("smtpPort");
            smtpPort.setCValue(setting.getSmtpPort() + "");
            save(smtpPort);
        } else {
            smtpPort.setCValue(setting.getSmtpPort() + "");
            update(smtpPort);
        }

        TUserSetting smtpUsername = maps.get("smtpUsername");
        if (smtpUsername == null) {
            smtpUsername = new TUserSetting();
            smtpUsername.setCKey("smtpUsername");
            smtpUsername.setCValue(setting.getSmtpUsername() + "");
            save(smtpUsername);
        } else {
            smtpUsername.setCValue(setting.getSmtpUsername() + "");
            update(smtpUsername);
        }

        TUserSetting smtpPassword = maps.get("smtpPassword");
        if (smtpPassword == null) {
            smtpPassword = new TUserSetting();
            smtpPassword.setCKey("smtpPassword");
            smtpPassword.setCValue(setting.getSmtpPassword() + "");
            save(smtpPassword);
        } else {
            smtpPassword.setCValue(setting.getSmtpPassword() + "");
            update(smtpPassword);
        }

        TUserSetting cookiePath = maps.get("cookiePath");
        if (cookiePath == null) {
            cookiePath = new TUserSetting();
            cookiePath.setCKey("cookiePath");
            cookiePath.setCValue(setting.getCookiePath() + "");
            save(cookiePath);
        } else {
            cookiePath.setCValue(setting.getCookiePath() + "");
            update(cookiePath);
        }

        TUserSetting cookieDomain = maps.get("cookieDomain");
        if (cookieDomain == null) {
            cookieDomain = new TUserSetting();
            cookieDomain.setCKey("cookieDomain");
            cookieDomain.setCValue(setting.getCookieDomain() + "");
            save(cookieDomain);
        } else {
            cookieDomain.setCValue(setting.getCookieDomain() + "");
            update(cookieDomain);
        }

        TUserSetting isDevelopmentEnabled = maps.get("isDevelopmentEnabled");
        if (isDevelopmentEnabled == null) {
            isDevelopmentEnabled = new TUserSetting();
            isDevelopmentEnabled.setCKey("isDevelopmentEnabled");
            isDevelopmentEnabled.setCValue(setting.getIsDevelopmentEnabled() + "");
            save(isDevelopmentEnabled);
        } else {
            isDevelopmentEnabled.setCValue(setting.getIsDevelopmentEnabled() + "");
            update(isDevelopmentEnabled);
        }

        TUserSetting registorPoint = maps.get("registorPoint");
        if (registorPoint == null) {
            registorPoint = new TUserSetting();
            registorPoint.setCKey("registorPoint");
            registorPoint.setCValue(setting.getRegistorPoint() + "");
            save(registorPoint);
        } else {
            registorPoint.setCValue(setting.getRegistorPoint() + "");
            update(registorPoint);
        }

        TUserSetting oldProfile = maps.get("oldProfile");
        if (oldProfile == null) {
            oldProfile = new TUserSetting();
            oldProfile.setCKey("oldProfile");
            oldProfile.setCValue(setting.getOldProfile() + "");
            save(oldProfile);
        } else {
            oldProfile.setCValue(setting.getOldProfile() + "");
            update(oldProfile);
        }

        TUserSetting addCustom = maps.get("addCustom");
        if (addCustom == null) {
            addCustom = new TUserSetting();
            addCustom.setCKey("addCustom");
            addCustom.setCValue(setting.getAddCustom() + "");
            save(addCustom);
        } else {
            addCustom.setCValue(setting.getAddCustom() + "");
            update(addCustom);
        }

        TUserSetting addCheck = maps.get("addCheck");
        if (addCheck == null) {
            addCheck = new TUserSetting();
            addCheck.setCKey("addCheck");
            addCheck.setCValue(setting.getAddCheck() + "");
            save(addCheck);
        } else {
            addCheck.setCValue(setting.getAddCheck() + "");
            update(addCheck);
        }

        TUserSetting addShare = maps.get("addShare");
        if (addShare == null) {
            addShare = new TUserSetting();
            addShare.setCKey("addShare");
            addShare.setCValue(setting.getAddShare() + "");
            save(addShare);
        } else {
            addShare.setCValue(setting.getAddShare() + "");
            update(addShare);
        }

        TUserSetting buyPoint = maps.get("buyPoint");
        if (buyPoint == null) {
            buyPoint = new TUserSetting();
            buyPoint.setCKey("buyPoint");
            buyPoint.setCValue(setting.getBuyPoint() + "");
            save(buyPoint);
        } else {
            buyPoint.setCValue(setting.getBuyPoint() + "");
            update(buyPoint);
        }

        TUserSetting sysTime = maps.get("sysTime");
        if (sysTime == null) {
            sysTime = new TUserSetting();
            sysTime.setCKey("sysTime");
            sysTime.setCValue(setting.getSysTime() + "");
            save(sysTime);
        } else {
            sysTime.setCValue(setting.getSysTime() + "");
            update(sysTime);
        }

        TUserSetting enableMachine = maps.get("enableMachine");
        if (enableMachine == null) {
            enableMachine = new TUserSetting();
            enableMachine.setCKey("enableMachine");
            enableMachine.setCValue(setting.getEnableMachine() + "");
            save(enableMachine);
        } else {
            enableMachine.setCValue(setting.getEnableMachine() + "");
            update(enableMachine);
        }

        TUserSetting totalUser = maps.get("totalUser");
        if (totalUser == null) {
            totalUser = new TUserSetting();
            totalUser.setCKey("totalUser");
            totalUser.setCValue(setting.getTotalUser() + "");
            save(totalUser);
        } else {
            totalUser.setCValue(setting.getTotalUser() + "");
            update(totalUser);
        }

        TUserSetting userBaseCount = maps.get("userBaseCount");
        if (userBaseCount == null) {
            userBaseCount = new TUserSetting();
            userBaseCount.setCKey("userBaseCount");
            userBaseCount.setCValue(setting.getUserBaseCount() + "");
            save(userBaseCount);
        } else {
            userBaseCount.setCValue(setting.getUserBaseCount() + "");
            update(userBaseCount);
        }

        TUserSetting randomStart = maps.get("randomStart");
        if (randomStart == null) {
            randomStart = new TUserSetting();
            randomStart.setCKey("randomStart");
            randomStart.setCValue(setting.getRandomStart() + "");
            save(randomStart);
        } else {
            randomStart.setCValue(setting.getRandomStart() + "");
            update(randomStart);
        }

        TUserSetting randomEnd = maps.get("randomEnd");
        if (randomEnd == null) {
            randomEnd = new TUserSetting();
            randomEnd.setCKey("randomEnd");
            randomEnd.setCValue(setting.getRandomEnd() + "");
            save(randomEnd);
        } else {
            randomEnd.setCValue(setting.getRandomEnd() + "");
            update(randomEnd);
        }

        TUserSetting qrCodeImagePath = maps.get("qrCodeImagePath");
        if (qrCodeImagePath == null) {
            qrCodeImagePath = new TUserSetting();
            qrCodeImagePath.setCKey("qrCodeImagePath");
            qrCodeImagePath.setCValue(setting.getQrCodeImagePath() + "");
            save(qrCodeImagePath);
        } else {
            qrCodeImagePath.setCValue(setting.getQrCodeImagePath() + "");
            update(qrCodeImagePath);
        }

        TUserSetting enableSystem = maps.get("enableSystem");
        if (enableSystem == null) {
            enableSystem = new TUserSetting();
            enableSystem.setCKey("enableSystem");
            enableSystem.setCValue(setting.getEnableSystem() + "");
            save(enableSystem);
        } else {
            enableSystem.setCValue(setting.getEnableSystem() + "");
            update(enableSystem);
        }

        TUserSetting enableBuySystem = maps.get("enableBuySystem");
        if (enableBuySystem == null) {
            enableBuySystem = new TUserSetting();
            enableBuySystem.setCKey("enableBuySystem");
            enableBuySystem.setCValue(setting.getEnableBuySystem() + "");
            save(enableBuySystem);
        } else {
            enableBuySystem.setCValue(setting.getEnableBuySystem() + "");
            update(enableBuySystem);
        }

        TUserSetting captchaTypes = maps.get("captchaTypes");
        Setting.CaptchaType[] type = setting.getCaptchaTypes();
        String types = "";
        int i = 0;
        for (Setting.CaptchaType item : type) {
            if (i != 0) {
                types += ",";
            }
            types += item.toString();
            i++;
        }
        if (captchaTypes == null) {
            captchaTypes = new TUserSetting();
            captchaTypes.setCKey("captchaTypes");
            captchaTypes.setCValue(types);
            save(captchaTypes);
        } else {
            captchaTypes.setCValue(types);
            update(captchaTypes);
        }

        CacheUtils.get().delete("setting");
    }

    @Override
    public String findServicePhone() {
        return tUserSettingDao.findServicePhone();
    }

}
